home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C20 / PriorityQueue3.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.3 KB  |  48 lines

  1. //: C20:PriorityQueue3.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // A more complex use of priority_queue
  7. #include <iostream>
  8. #include <queue>
  9. #include <string>
  10. using namespace std;
  11.  
  12. class ToDoItem {
  13.   char primary;
  14.   int secondary;
  15.   string item;
  16. public:
  17.   ToDoItem(string td, char pri ='A', int sec =1)
  18.     : item(td), primary(pri), secondary(sec) {}
  19.   friend bool operator<(
  20.     const ToDoItem& x, const ToDoItem& y) {
  21.     if(x.primary > y.primary) 
  22.       return true;
  23.     if(x.primary == y.primary)
  24.       if(x.secondary > y.secondary) 
  25.         return true;
  26.     return false;
  27.   }
  28.   friend ostream& 
  29.   operator<<(ostream& os, const ToDoItem& td) {
  30.     return os << td.primary << td.secondary 
  31.       << ": " << td.item;
  32.   }
  33. };
  34.  
  35. int main() {
  36.   priority_queue<ToDoItem> toDoList;
  37.   toDoList.push(ToDoItem("Empty trash", 'C', 4));
  38.   toDoList.push(ToDoItem("Feed dog", 'A', 2));
  39.   toDoList.push(ToDoItem("Feed bird", 'B', 7));
  40.   toDoList.push(ToDoItem("Mow lawn", 'C', 3));
  41.   toDoList.push(ToDoItem("Water lawn", 'A', 1));
  42.   toDoList.push(ToDoItem("Feed cat", 'B', 1));
  43.   while(!toDoList.empty()) {
  44.     cout << toDoList.top() << endl;
  45.     toDoList.pop();
  46.   }
  47. } ///:~
  48.